﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class admin_role : System.Web.UI.Page
{   
    string pperfil;
    DataSet perfiles;
    Table tablaPanel, tablaPermit = null;
    TableRow rowPage;
    rUser userinfo;
    RE_GenericBean rpage = new RE_GenericBean();
    protected void Page_Load(object sender, EventArgs e)
    {
         if (Session["userid"] == null) { Response.Redirect("../Default.aspx"); }
        //Si es la primera vez que entra


         if (!Page.IsPostBack)
         {
             rpage.intC1 = 0;
             rpage.intC2 = 0;
             Session["rpage"] = rpage;
             com.bpophotos.www.Service service = new com.bpophotos.www.Service();
             pperfil = Request.QueryString["Id"].ToString();
             if (pperfil == null) { Response.Redirect("mroles.aspx;"); }
             else
             {
                 if (int.Parse(pperfil) > 1)
                 {
                     if (Session["PerfilesPermiso"] != null)
                     {
                         perfiles = (DataSet)Session["PerfilesPermiso"];
                     }
                     else
                     {
                         string rxml = service.getPerfilesPermiso(1);
                         perfiles = Utility.getPerfilesPermiso(rxml);
                     }
                 }
                 else
                 {
                     if (Session["ProfilePermisos"] != null)
                     {
                         perfiles = (DataSet)Session["ProfilePermisos"];
                     }
                     else
                     {
                         string rxml = service.getPerfilesPermiso(0);
                         perfiles = Utility.getPerfilesPermiso(rxml);
                     }
                 }
             }

             if(perfiles != null)
             {
                 
                 for (int i = 0; i <= (perfiles.Tables[0].Rows.Count - 1); i++)
                 {
                     if (perfiles.Tables[0].Rows[i]["per_id"].ToString().Equals(pperfil))
                     {
                         tb_role.Text = perfiles.Tables[0].Rows[i]["per_nombre"].ToString();
                     }
                 }
                 long permisoperfil = 0; long restrictedperfil = 0;
                 
                 string sper = service.getPerfilesPermiso(1);
                // string sper = "";
                 DataSet dper = Utility.getPerfilesPermiso(sper);
                 for (int i = 0; i <= (dper.Tables[0].Rows.Count - 1); i++)
                 {
                     if (dper.Tables[0].Rows[i]["per_id"].ToString().Equals(pperfil))
                     {
                         string b = perfiles.Tables[0].Rows[i]["per_permiso"].ToString();
                         if (b.Equals(""))
                         {
                             permisoperfil = 0;
                         }
                         else
                         {
                             permisoperfil = long.Parse(b);
                         }

                         string a = perfiles.Tables[0].Rows[i]["per_restricted"].ToString();
                         if (a.Equals(""))
                         {
                             restrictedperfil = 0;
                         }
                         else
                         {
                             restrictedperfil = long.Parse(a);
                         }
                     }
                 }

                 fillAccessnew(0, 0);
                 //comprobar();
             }
         }
         else {
             rpage = (RE_GenericBean)Session["rpage"];
             if (rpage != null)
             {
                 fillAccessnew(rpage.intC1, rpage.intC2);
             }
             string aux = (string)Session["PageIDRole"];
             if (aux != null)
             {
                 crearPagPermisos(aux);
                 Session["PageIDRole"] = null;
             }
         }
    }

    private string crearxml()
    {
       string result="";
       pperfil = Request.QueryString["Id"].ToString();
       result += "<?xml version=\"1.0\" encoding=\"utf-8\"?><data><perfilid>" + pperfil + "</perfilid><app>1</app><perfilname>"+tb_role.Text+"</perfilname>";
      
       DataSet dsauxper = (DataSet)(Session["dsPermitRoles"]);
       Panel panelAccess = Punaccess;

        if(dsauxper != null)
        {   
            for (int a = 0; a < dsauxper.Tables[0].Rows.Count; a++)
            {
                string tableID = "tabla" + dsauxper.Tables[0].Rows[a]["id_page"].ToString();
                Table tableaux = ((Table)puppermitRole.FindControl(tableID));
                if (tableaux != null)
                {
                    int paginaid = int.Parse(dsauxper.Tables[0].Rows[a]["id_page"].ToString());
                    result = result + "<page><pageid>" + paginaid + "</pageid><OptionsAccess>";

                    int page = 0;
                    //rl2.ID = "option" + oppPage.Tables[1].Rows[i]["id_option"].ToString();
                    RadioButtonList rUnlimitted = ((RadioButtonList)Accordion2.FindControl("page" + paginaid));
                    if (rUnlimitted != null)
                    {
                        string n = dsauxper.Tables[0].Rows[a]["permit"].ToString();

                        if (rUnlimitted.Items[0].Selected == true)
                        {
                            result += "<pageyes>" + n + " </pageyes>";
                        }
                    }
                    //buscando opciones de esta pagina
                    for (int b = 0; b < dsauxper.Tables[1].Rows.Count; b++)
                    {
                        string optioname = dsauxper.Tables[1].Rows[b]["name_option"].ToString();
                        string temp2 = dsauxper.Tables[1].Rows[b]["id_option"].ToString();
                        int temppage = int.Parse(dsauxper.Tables[1].Rows[b]["id_page"].ToString());
                        if (temppage == paginaid)
                        {
                            RadioButtonList rUnlimitted2 = ((RadioButtonList)Accordion2.FindControl("option" + temp2));

                            if (rUnlimitted2 != null)
                            {
                                string n = dsauxper.Tables[1].Rows[b]["permit"].ToString();
                                if (rUnlimitted2.Items[0].Selected == true && rUnlimitted2 != null)
                                {

                                    result += "<optionyes>" + n + " </optionyes>";
                                }
                            }
                        }
                    }
                    result += "</OptionsAccess>";
                    result += "<RestrictedAccess>";
                    RadioButtonList rRestricted = ((RadioButtonList)Accordion2.FindControl("page" + paginaid));

                    if (rRestricted != null)
                    {
                        string n = dsauxper.Tables[0].Rows[a]["permit"].ToString();
                        if (rRestricted.Items[2].Selected == true)
                        {
                            //result += "<optionyes>" + n + " </optionyes>";
                            result += "<pageyes>" + n + " </pageyes>";
                        }
                    }
                    for (int b = 0; b < dsauxper.Tables[1].Rows.Count; b++)
                    {

                        int temp3 = int.Parse(dsauxper.Tables[1].Rows[b]["id_option"].ToString());
                        RadioButtonList rRestricted2 = ((RadioButtonList)Accordion2.FindControl("option" + temp3));
                        int temppage = int.Parse(dsauxper.Tables[1].Rows[b]["id_page"].ToString());
                        if (temppage == paginaid)
                        {
                            if (rRestricted2 != null)
                            {
                                string n = dsauxper.Tables[1].Rows[b]["permit"].ToString();
                                if (rRestricted2.Items[2].Selected == true)
                                {

                                    result += "<optionyes>" + n + " </optionyes>";
                                }
                            }
                        }
                    }

                    result += "</RestrictedAccess>";
                    result += "</page>";
                }
            }//fin del for pages

            }// fin si ds no es null
           
       result += "</data>";
       return result;
    }
    
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {}

    private void fillAccessnew(int pgnivel, int pgnum)
    {
        try
        {
            string rxml = null, rxml2 = null;
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            rxml2 = service.getPagesView(1);

            if (!rxml2.Equals("") || rxml2 != null)
            {
                DataSet dsPages = Utility.getPagesView(rxml2);
                Session["dsPagesView"] = dsPages;
                rxml = service.getAppPages(1);

                if (!rxml.Equals("") || rxml != null)
                {
                    DataSet dsclient = Utility.getPermitPage(rxml);
                    if (dsclient != null)
                    {
                        Session["dsPermitRoles"] = dsclient;

                        if (dsclient.Tables[0].Columns.Count != 0)
                        {
                            Punaccess.Controls.Clear();
                            tablaPanel = new Table();
                            tablaPanel.Width = 720;
                            tablaPanel.CellPadding = 5;
                            tablaPanel.HorizontalAlign = HorizontalAlign.Left;
                            tablaPanel.Font.Size = 9;
                            tablaPanel.Font.Name = "Arial";
                            tablaPanel.BorderStyle = BorderStyle.Ridge;
                            tablaPanel.BorderColor = System.Drawing.Color.Black;
                            tablaPanel.BorderWidth = 3;
                            tablaPanel.CssClass = "bodyPag";
                            tablaPanel.Attributes.Add("Style", "box-shadow: 0 0 20px 3px #000000;");

                            if (pgnivel == 0)
                            {
                                for (int j = 0; j < dsPages.Tables[0].Rows.Count; j++)
                                {
                                    if (int.Parse(dsPages.Tables[0].Rows[j]["type"].ToString()) == 1)
                                    {
                                        for (int i = 0; i < dsclient.Tables[0].Rows.Count; i++)
                                        {
                                            if (int.Parse(dsPages.Tables[0].Rows[j]["page_id"].ToString()) == int.Parse(dsclient.Tables[0].Rows[i]["id_page"].ToString()))
                                            {
                                                loadImaPages((j + 1), dsclient.Tables[0].Rows[i]["id_page"].ToString(), dsclient.Tables[0].Rows[i]["name_page"].ToString(), dsclient.Tables[0].Rows[i]["image"].ToString());
                                                break;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        loadImaPages((j + 1), ("1_" + dsPages.Tables[0].Rows[j]["id"].ToString()), dsPages.Tables[0].Rows[j]["name"].ToString(), dsPages.Tables[0].Rows[j]["url"].ToString());
                                    }
                                }
                            }
                            else
                            {
                                int contp = 0;
                                for (int j = 0; j < dsPages.Tables[pgnivel].Rows.Count; j++)
                                {
                                    string pageid = "pg" + pgnivel + "_id";
                                    if (int.Parse(dsPages.Tables[pgnivel].Rows[j][pageid].ToString()) == pgnum)
                                    {
                                        contp++;
                                        if (int.Parse(dsPages.Tables[pgnivel].Rows[j]["type"].ToString()) == 1)
                                        {
                                            for (int i = 0; i < dsclient.Tables[0].Rows.Count; i++)
                                            {
                                                if (int.Parse(dsPages.Tables[pgnivel].Rows[j]["page_id"].ToString()) == int.Parse(dsclient.Tables[0].Rows[i]["id_page"].ToString()))
                                                {
                                                    loadImaPages(contp, dsclient.Tables[0].Rows[i]["id_page"].ToString(), dsclient.Tables[0].Rows[i]["name_page"].ToString(), dsclient.Tables[0].Rows[i]["image"].ToString());
                                                    break;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            loadImaPages(contp, ("2_" + dsPages.Tables[pgnivel].Rows[j]["id"].ToString()), dsPages.Tables[pgnivel].Rows[j]["name"].ToString(), dsPages.Tables[pgnivel].Rows[j]["url"].ToString());
                                        }
                                    }
                                }
                            }
                        }
                        Punaccess.Controls.Add(tablaPanel);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Utility.writeError("Error al obtener datos paginas WEBSERVICE role.aspx " + ex.Message + " " + ex.StackTrace);
        }
    }

    private void loadImaPages(int contPage, string idPage, string namePage, string imaPage)
    {
        if (contPage == 1)
        {
            rowPage = new TableRow();
            tablaPanel.Controls.Add(rowPage);
        }
        if ((contPage % 4) == 0)
        {
            rowPage.Cells.Add(oldRowIma(contPage, idPage, namePage, imaPage));
            tablaPanel.Controls.Add(rowPage);
            rowPage = new TableRow();
        }
        else
        {
            rowPage.Cells.Add(oldRowIma(contPage, idPage, namePage, imaPage));
            tablaPanel.Controls.Remove(rowPage);
            tablaPanel.Controls.Add(rowPage);
        }
    }

    private TableCell oldRowIma(int contPage, string idPage, string namePage, string imaPage)
    {
        TableCell result = new TableCell();

        Table table = new Table();
        TableRow row1 = new TableRow();
        TableCell cell1 = new TableCell();
        cell1.Width = 120;
        cell1.Height = 65;
        cell1.HorizontalAlign = HorizontalAlign.Center;
        ImageButton imaButton = new ImageButton();
        imaButton.ID = "bt_" + idPage;
        imaButton.BorderWidth = 0;

        string auxOver = imaPage.Replace(".", "2.");
        imaButton.Attributes.Add("onmouseout", "this.src='../img/ImaPages/" + imaPage + "'");
        imaButton.Attributes.Add("onmouseover", "this.src='../img/ImaPages/" + auxOver + "'");

        if (!imaPage.Equals(""))
        {
            imaButton.ImageUrl = "../img/ImaPages/" + imaPage;
        }
        imaButton.Click += new ImageClickEventHandler(imaButton_Click);
        cell1.Attributes.Add("Style", "background: url(../img/ImaPages/base.png) no-repeat center bottom;");
        cell1.Controls.Add(imaButton);
        row1.Cells.Add(cell1);
        table.Controls.Add(row1);

        TableRow row2 = new TableRow();

        TableCell cell2 = new TableCell();
        cell2.Width = 120;
        cell2.Height = 50;
        cell2.HorizontalAlign = HorizontalAlign.Center;
        cell2.VerticalAlign = VerticalAlign.Top;
        Label imaLabel = new Label();
        imaLabel.Text = namePage;
        imaLabel.Font.Bold = true;
        cell2.Controls.Add(imaLabel);
        row2.Cells.Add(cell2);
        table.Controls.Add(row2);

        result.Controls.Add(table);
        return result;
    }

    void imaButton_Click(object sender, ImageClickEventArgs e)
    {
        Session["PageIDRole"] = null;
        ImageButton ibt = (ImageButton)sender;
        string ID = ibt.ID;
        string[] pID = ID.Split('_');
        if (pID.Length < 3)
        {
            crearPagPermisos(ID);
            Session["PageIDRole"] = ID;
            bthide_ModalPopupExtender.Show();
        }
        else
        {
            //rpage.intC3 = rpage.intC2;
            rpage.intC1 = int.Parse(pID[1]);
            rpage.intC2 = int.Parse(pID[2]);
            Session["rpage"] = rpage;
            fillAccessnew(rpage.intC1, rpage.intC2);
            lnk_back.Visible = true;
        }
    }

    public void mostrarPermisos(int tabla, DataSet dsclient, string perfil)
    {
        com.bpophotos.www.Service service = new com.bpophotos.www.Service();
       
        if (perfil != null && perfil != "")
        {
            int per = int.Parse(perfil);
            Utility.writeError("**** Perfil lleva  " + perfil);
            string resp = service.getPerfilPerRes(per, "1");
            //string resp = "";
            DataSet re = Utility.PharseXML_PerRes(resp);
            long useraccess = 0; long userrestricted = 0;
            
            Panel panelAccess = Punaccess;

            for (int j = 0; j < dsclient.Tables[tabla].Rows.Count; j++)
            {
                string campo = "";
                if (tabla == 0) { campo = "id_page"; } else if (tabla == 1) { campo = "id_option"; }
                
                for (int k = 0; k < re.Tables[0].Rows.Count; k++)
                {
                    /*string aux1 = dsclient.Tables[tabla].Rows[j][campo].ToString();
                    string aux2 = re.Tables[0].Rows[k]["page_id"].ToString();
                    string aux3 = dsclient.Tables[1].Rows[j]["name_option"].ToString();*/
                    if (dsclient.Tables[tabla].Rows[j]["id_page"].ToString() == re.Tables[0].Rows[k]["page_id"].ToString())
                    {
                        long papp1 = long.Parse(dsclient.Tables[tabla].Rows[j]["permit"].ToString());
                        useraccess = long.Parse(re.Tables[0].Rows[k]["per_permiso"].ToString());
                        userrestricted = long.Parse(re.Tables[0].Rows[k]["per_restricted"].ToString());

                        long papp2 = papp1 & useraccess;
                        long papp3 = papp1 & userrestricted;
                        string rbID = "";
                        if (tabla == 0) { rbID = "page" + dsclient.Tables[0].Rows[j]["id_page"].ToString(); } else if (tabla == 1) { rbID = "option" + dsclient.Tables[1].Rows[j]["id_option"].ToString(); }
                        if (papp2 == papp1)
                        {
                            if (papp3 != papp1)
                            {
                                RadioButtonList rUnlimitted = ((RadioButtonList)Accordion2.FindControl(rbID));
                                if (rUnlimitted != null)
                                {
                                    rUnlimitted.Items[0].Selected = true;
                                    rUnlimitted.Items[0].Attributes.Add("style", "color:green");
                                }
                            }
                        }

                        if (papp1 != papp2)
                        {
                            if (papp1 != papp3)
                            {
                                RadioButtonList rViewOnly = ((RadioButtonList)Accordion2.FindControl(rbID));
                                if (rViewOnly != null)
                                {
                                    rViewOnly.Items[1].Selected = true;
                                    rViewOnly.Items[1].Attributes.Add("style", "color:#FA7E02");
                                }
                            }
                        }

                        if (papp1 == papp3)
                        {
                            RadioButtonList rRestricted = ((RadioButtonList)Accordion2.FindControl(rbID));
                            if (rRestricted != null)
                            {
                                rRestricted.Items[2].Selected = true;
                                rRestricted.Items[2].Attributes.Add("style", "color:red");
                            }
                        }
                        break;
                    }
                }
            }
        }
    }

    private void crearPagPermisos(string pageID)
    {
        DataSet dsclient = (DataSet)Session["dsPermitRoles"];

        string[] pID = pageID.Split('_');
        if (dsclient != null)
        {
            for (int i = 0; i < dsclient.Tables[0].Rows.Count; i++)
            {
                if (dsclient.Tables[0].Rows[i]["id_page"].ToString() == pID[1].Trim())
                {
                    if (puppermitRole.Controls.Contains(tablaPermit))
                        puppermitRole.Controls.Remove(tablaPermit);
                    tablaPermit = new Table();
                    //tablaPermit.Width = 450;
                    tablaPermit.CellPadding = 5;
                    tablaPermit.HorizontalAlign = HorizontalAlign.Left;
                    tablaPermit.Font.Size = 9;
                    tablaPermit.Font.Name = "Arial";
                    TableRow row1 = new TableRow();
                    row1.Cells.Add(rowPagina(dsclient.Tables[0].Rows[i]["id_page"].ToString(), dsclient.Tables[0].Rows[i]["name_page"].ToString(), dsclient.Tables[0].Rows[i]["image"].ToString()));
                    tablaPermit.Controls.Add(row1);
                    //TableRow rows = new TableRow();

                    puppermitRole.Controls.Add(tablaPermit);
                    newSection(dsclient.Tables[0].Rows[i]["id_page"].ToString());
                    newPage(dsclient.Tables[0].Rows[i]["id_page"].ToString(), dsclient.Tables[0].Rows[i]["name_page"].ToString(), dsclient.Tables[0].Rows[i]["permit"].ToString(), dsclient);

                    //tablaPermit.Controls.Add(rows);
                    TableRow rows2 = new TableRow();
                    TableCell cell = new TableCell();
                    cell.Controls.Add(lnk_save);
                    cell.Controls.Add(lnk_cancel);
                    rows2.Cells.Add(cell);
                    rows2.Attributes.Add("Style", "text-align:center");
                    tablaPermit.Controls.Add(rows2);
                    tablaPermit.ID = "tabla" + dsclient.Tables[0].Rows[i]["id_page"].ToString();
                    //tablaPermit.Attributes.Add("Style", "margin:7px; width:410px");
                    break;
                }
            }

            puppermitRole.BorderColor = System.Drawing.Color.Black;
            puppermitRole.BorderStyle = BorderStyle.Ridge;

            pperfil = Request.QueryString["Id"].ToString();
            mostrarPermisos(0, dsclient, pperfil);
            mostrarPermisos(1, dsclient, pperfil);
        }
    }

    private TableCell rowPagina(string idPage, string namePage, string imaPage)
    {
        TableCell result = new TableCell();

        Table table = new Table();
        TableRow row1 = new TableRow();
        TableCell cell1 = new TableCell();
        cell1.Width = 60;
        cell1.Height = 60;
        cell1.HorizontalAlign = HorizontalAlign.Left;
        ImageButton imageButton = new ImageButton();
        imageButton.ID = "btn_" + idPage;
        imageButton.BorderWidth = 0;

        string auxOver = imaPage.Replace(".", "2.");
        imageButton.Attributes.Add("onmouseout", "this.src='../img/ImaPages/" + imaPage + "'");
        imageButton.Attributes.Add("onmouseover", "this.src='../img/ImaPages/" + auxOver + "'");

        if (!imaPage.Equals(""))
        {
            imageButton.ImageUrl = "../img/ImaPages/" + imaPage;
        }
        cell1.Controls.Add(imageButton);
        row1.Cells.Add(cell1);
        table.Controls.Add(row1);

        TableCell cell2 = new TableCell();
        cell2.Width = 350;
        cell2.Height = 20;
        cell2.HorizontalAlign = HorizontalAlign.Left;
        Label imaLabel = new Label();
        imaLabel.Text = namePage;
        imaLabel.Font.Bold = true;
        cell2.Controls.Add(imaLabel);
        cell2.Font.Size = 14;
        cell2.Font.Name = "Arial";
        row1.Cells.Add(cell2);

        TableRow row2 = new TableRow();
        TableCell cell3 = new TableCell();
        cell3.Controls.Add(allaccess);
        allaccess.Visible = false;
        cell3.ColumnSpan = 2;
        cell3.Font.Size = 9;
        cell3.Font.Name = "Arial";
        row2.Cells.Add(cell3);

        table.Controls.Add(row2);

        result.Controls.Add(table);
        return result;
    }

    public void newPage(string id_parent, string name_page, string per_page, DataSet oppPage)
    {
        DataSet section = null;
        section = DB.getSectionPage(id_parent);
        /* Devuelve una tabla para cada pagina
        TableCell result = new TableCell();
        Table tabla = new Table();*/

        // Permisos de la pagina
        TableRow rowPer = new TableRow();

        TableCell cellblank = new TableCell();
        cellblank.Width = 140;
        //cellblank.BorderStyle = BorderStyle.Groove;
        //cellblank.BorderWidth = 2;
        Label lnamepage = new Label();
        lnamepage.Text = name_page;
        cellblank.Controls.Add(lnamepage);
        rowPer.Cells.Add(cellblank);

        TableCell cellPer = new TableCell();
        cellPer.HorizontalAlign = HorizontalAlign.Left;
        RadioButtonList rl1 = new RadioButtonList();
        rl1.ID = "page"+id_parent;
        rl1.Items.Add("Unlimited");
        rl1.Items.Add("View Only");
        rl1.Items.Add("Restricted");
        //rl1.Font.Bold = true;

        // Diseño y aparencia de la opcion
        /*rl1.Items[0].Attributes.Add("onmouseover", "this.style.color='green';");
        rl1.Items[0].Attributes.Add("onmouseout", "this.style.color='black';");
        rl1.Items[1].Attributes.Add("onmouseover", "this.style.color='#FA7E02';");
        rl1.Items[1].Attributes.Add("onmouseout", "this.style.color='black';");
        rl1.Items[2].Attributes.Add("onmouseover", "this.style.color='red';");
        rl1.Items[2].Attributes.Add("onmouseout", "this.style.color='black';");*/
        rl1.RepeatDirection = RepeatDirection.Horizontal;

        cellPer.Controls.Add(rl1);
        rowPer.Cells.Add(cellPer);

        Table tableG = (Table)Accordion2.FindControl("TableSecGeneral");
        if (tableG != null)
            tableG.Controls.Add(rowPer);
        int contOpp = 0;
        TableRow rowOpp = null;
        // Opciones de la pagina
        for (int i = 0; i < oppPage.Tables[1].Rows.Count; i++)
        {
            if (oppPage.Tables[1].Rows[i]["id_page"].ToString() == id_parent)
            {
                if (section.Tables[0].Rows.Count > 1)
                {
                    contOpp++;
                    if ((contOpp % 2) != 0)
                        rowOpp = new TableRow();
                }
                else { rowOpp = new TableRow(); }

                // Nueva celda nombre opcion
                TableCell cellOpp = new TableCell();
                cellOpp.Width = 140;
                cellOpp.HorizontalAlign = HorizontalAlign.Left;
                Label lOpp = new Label();
                lOpp.Width = 140;
                lOpp.Text = oppPage.Tables[1].Rows[i]["name_option"].ToString();
                lOpp.Font.Bold = true;
                lOpp.Font.Size = 9;
                lOpp.Font.Name = "Arial";
                //lOpp.ForeColor = System.Drawing.Color.FromName("#525663");
                cellOpp.Controls.Add(lOpp);
                rowOpp.Cells.Add(cellOpp);

                // Nueva celda opcion
                TableCell cellOpp2 = new TableCell();
                cellOpp2.HorizontalAlign = HorizontalAlign.Left;
                RadioButtonList rl2 = new RadioButtonList();
                rl2.ID = "option" + oppPage.Tables[1].Rows[i]["id_option"].ToString();
                rl2.Items.Add("Unlimited");
                rl2.Items.Add("View Only");
                rl2.Items.Add("Restricted");

                // Diseño y aparencia de la opcion
                rl2.ForeColor = System.Drawing.Color.FromName("#525663");

                /*rl2.Items[0].Attributes.Add("onmouseover", "this.style.color='green';");
                rl2.Items[0].Attributes.Add("onmouseout", "this.style.color='#525663';");
                rl2.Items[1].Attributes.Add("onmouseover", "this.style.color='#FA7E02';");
                rl2.Items[1].Attributes.Add("onmouseout", "this.style.color='#525663';");
                rl2.Items[2].Attributes.Add("onmouseover", "this.style.color='red';");
                rl2.Items[2].Attributes.Add("onmouseout", "this.style.color='#525663';");*/
                rl2.RepeatDirection = RepeatDirection.Horizontal;

                cellOpp2.Controls.Add(rl2);
                //cellOpp2.BorderStyle = BorderStyle.Groove;
                //cellOpp2.BorderWidth = 2;
                rowOpp.Cells.Add(cellOpp2);

                Table tableG2 = null;
                if (oppPage.Tables[1].Rows[i]["name_section"].ToString().Equals("General"))
                {
                    tableG2 = (Table)Accordion2.FindControl("TableSecGeneral");
                }
                else
                {
                    tableG2 = (Table)Accordion2.FindControl("TableSec" + oppPage.Tables[1].Rows[i]["id_section"].ToString().Trim());
                }
                if (tableG2 != null)
                {
                    tableG2.Controls.Add(rowOpp);
                    if (i < (oppPage.Tables[1].Rows.Count - 1))
                        if (int.Parse(oppPage.Tables[1].Rows[i]["id_section"].ToString()) != int.Parse(oppPage.Tables[1].Rows[i + 1]["id_section"].ToString()))
                        {
                            rowOpp = new TableRow();
                            contOpp = 0;
                        }
                    if (int.Parse(oppPage.Tables[1].Rows[i]["orden"].ToString()) == 0)
                    {
                        contOpp = 0;
                    }
                }
            }
        }
        //return result;
    }

    public void newSection(string id_parent)
    {
        DataSet section = null;
        section = DB.getSectionPage(id_parent);
        //***************************** Section *************************************************
        if (true)
        {
            Table tableP = new Table();
            // Tamaño y alineacion de la tabla
            if (section.Tables[0].Rows.Count > 1)
                tableP.Width = 820;
            tableP.CellSpacing = 0;
            tableP.HorizontalAlign = HorizontalAlign.Left;
            // Nombre y estilo de la tabla
            tableP.BorderStyle = BorderStyle.Groove;
            tableP.BorderWidth = 2;
            tableP.Font.Bold = true;

            TableRow rowP = new TableRow();
            TableCell cellP = new TableCell();
            Accordion2.Panes.Clear();
            cellP.Controls.Add(Accordion2);
            rowP.Cells.Add(cellP);
            tableP.Controls.Add(rowP);

            TableRow rowP2 = new TableRow();
            TableCell cellP2 = new TableCell();
            cellP2.Controls.Add(tableP);
            rowP2.Cells.Add(cellP2);
            tablaPermit.Controls.Add(rowP2);

            if (section != null)
            {
                for (int j = 0; j < section.Tables[0].Rows.Count; j++)
                {
                    Table tableS = new Table();

                    if (section.Tables[0].Rows[j]["section_name"].ToString().Trim().Equals("General"))
                    {
                        tableS.ID = "TableSecGeneral";
                    }
                    else
                    {
                        tableS.ID = "TableSec" + section.Tables[0].Rows[j]["section_id"].ToString().Trim();
                    }
                    tableS.CellSpacing = 1;
                    tableS.HorizontalAlign = HorizontalAlign.Left;
                    /*tableS.BorderStyle = BorderStyle.Solid;
                    tableS.BorderColor = System.Drawing.Color.Red;
                    tableS.BorderWidth = 2;*/
                    if (section.Tables[0].Rows.Count > 1)
                        tableS.Width = 820;
                    else
                        tableS.Width = 410;
                    Label lsection = new Label();
                    lsection.ID = "ls" + section.Tables[0].Rows[j]["section_id"].ToString();
                    lsection.Text = section.Tables[0].Rows[j]["section_name"].ToString();
                    lsection.Font.Size = 10;
                    TableCell c1 = new TableCell();
                    c1.HorizontalAlign = HorizontalAlign.Left;
                    c1.Controls.Add(lsection);
                    TableRow r1 = new TableRow();
                    r1.Cells.Add(c1);
                    if (!section.Tables[0].Rows[j]["section_name"].ToString().Trim().Equals("General"))
                        tableS.Controls.Add(r1);

                    AjaxControlToolkit.AccordionPane ap = new AjaxControlToolkit.AccordionPane();
                    ap.ID = "AP" + section.Tables[0].Rows[j]["section_id"].ToString();
                    ap.Controls[0].Controls.Add(lsection);
                    ap.Controls[1].Controls.Add(tableS);
                    Accordion2.Panes.Add(ap);
                }
            }
            Accordion2.RequireOpenedPane = true;
            Accordion2.SelectedIndex = 0;
        }
        else { section = null; }
    }

    protected void allaccess_SelectedIndexChanged(object sender, EventArgs e)
    {
      
        DataSet dsclien = (DataSet)Session["dsPermitRoles"];
        Panel panelAccess = Punaccess;
        if (allaccess.Items[0].Selected == true)
        {
            allaccess.Items[0].Attributes.Add("style", "color:green");
            for (int a = 0; a < dsclien.Tables[0].Rows.Count; a++)
            {
                string buscar = "page" + dsclien.Tables[0].Rows[a]["id_page"].ToString();

                RadioButtonList rUnlimitted = ((RadioButtonList)panelAccess.FindControl(buscar));
                rUnlimitted.Items[2].Selected = false;
                rUnlimitted.Items[2].Attributes.Add("style", "color:black");
                rUnlimitted.Items[1].Selected = false;
                rUnlimitted.Items[1].Attributes.Add("style", "color:black");
                rUnlimitted.Items[0].Selected = true;
                rUnlimitted.Items[0].Attributes.Add("style", "color:green");
            }
            for (int b = 0; b < dsclien.Tables[1].Rows.Count; b++)
            {
                string buscar = "option" + dsclien.Tables[1].Rows[b]["id_option"].ToString();
                RadioButtonList rUnlimitted = ((RadioButtonList)panelAccess.FindControl(buscar));
                rUnlimitted.Items[2].Selected = false;
                rUnlimitted.Items[2].Attributes.Add("style", "color:#525663");
                rUnlimitted.Items[1].Selected = false;
                rUnlimitted.Items[1].Attributes.Add("style", "color:#525663");
                rUnlimitted.Items[0].Selected = true;
                rUnlimitted.Items[0].Attributes.Add("style", "color:green");
            }
        }
        if (allaccess.Items[1].Selected == true)
        {
            allaccess.Items[1].Attributes.Add("style", "color:#FA7E02");
            for (int a = 0; a < dsclien.Tables[0].Rows.Count; a++)
            {
                string buscar = "page" + dsclien.Tables[0].Rows[a]["id_page"].ToString();

                RadioButtonList rViewOnly = ((RadioButtonList)panelAccess.FindControl(buscar));
                rViewOnly.Items[2].Selected = false;
                rViewOnly.Items[2].Attributes.Add("style", "color:black");
                rViewOnly.Items[0].Selected = false;
                rViewOnly.Items[0].Attributes.Add("style", "color:black");
                rViewOnly.Items[1].Selected = true;
                rViewOnly.Items[1].Attributes.Add("style", "color:#FA7E02");
            }
            for (int b = 0; b < dsclien.Tables[1].Rows.Count; b++)
            {
                string buscar = "option" + dsclien.Tables[1].Rows[b]["id_option"].ToString();

                RadioButtonList rViewOnly = ((RadioButtonList)panelAccess.FindControl(buscar));
                rViewOnly.Items[2].Selected = false;
                rViewOnly.Items[2].Attributes.Add("style", "color:#525663");
                rViewOnly.Items[0].Selected = false;
                rViewOnly.Items[0].Attributes.Add("style", "color:#525663");
                rViewOnly.Items[1].Selected = true;
                rViewOnly.Items[1].Attributes.Add("style", "color:#FA7E02");
            }
        }
        if (allaccess.Items[2].Selected == true)
        {
            allaccess.Items[2].Attributes.Add("style", "color:red");
            for (int a = 0; a < dsclien.Tables[0].Rows.Count; a++)
            {
                string buscar = "page" + dsclien.Tables[0].Rows[a]["id_page"].ToString();

                RadioButtonList rRestricted = ((RadioButtonList)panelAccess.FindControl(buscar));
                rRestricted.Items[0].Selected = false;
                rRestricted.Items[0].Attributes.Add("style", "color:black");
                rRestricted.Items[1].Selected = false;
                rRestricted.Items[1].Attributes.Add("style", "color:black");
                rRestricted.Items[2].Selected = true;
                rRestricted.Items[2].Attributes.Add("style", "color:red");
            }
            for (int b = 0; b < dsclien.Tables[1].Rows.Count; b++)
            {
                string buscar = "option" + dsclien.Tables[1].Rows[b]["id_option"].ToString();

                RadioButtonList rRestricted = ((RadioButtonList)panelAccess.FindControl(buscar));
                rRestricted.Items[0].Selected = false;
                rRestricted.Items[0].Attributes.Add("style", "color:#525663");
                rRestricted.Items[1].Selected = false;
                rRestricted.Items[1].Attributes.Add("style", "color:#525663");
                rRestricted.Items[2].Selected = true;
                rRestricted.Items[2].Attributes.Add("style", "color:red");
            }
        }
    
    }

    protected void lnk_save_Click(object sender, EventArgs e)
    {
        string sxml = crearxml();
        com.bpophotos.www.Service service = new com.bpophotos.www.Service();
        service.updateDataPerfil(sxml);
        service.Dispose();
    }

    protected void lnk_delete_Click(object sender, EventArgs e)
    {
        pperfil = Request.QueryString["Id"].ToString();
        if (pperfil != null)
            Response.Redirect("deleteRole.aspx?id=" + pperfil);
    }
    protected void lnk_back_Click(object sender, EventArgs e)
    {
        /*rpage = (RE_GenericBean)Session["rpage"];
        if (rpage.intC1 == 1)
        {*/
            rpage.intC1 = 0; rpage.intC2 = 0; rpage.intC3 = 0;
           /* fillAccessnew(rpage.intC1, rpage.intC2);
            lnk_back.Visible = false;
        }
        else if (rpage.intC1 == 2)
        {
            rpage.intC1 = rpage.intC1 - 1;
            fillAccessnew(rpage.intC1, rpage.intC3);
        }*/
        Session["rpage"] = rpage;
        Response.Redirect(Request.UrlReferrer.ToString());
    }
}